package se.peterbjorkman.android.trafikkamera;

import java.util.List;

import se.peterbjorkman.android.trafikkamera.CameraMapOverlay.OnTapListener;
import se.peterbjorkman.android.trafikkamera.providers.CameraContentProvider;
import se.peterbjorkman.android.trafikkamera.providers.CameraDefs;

import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.database.Cursor;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapView;
import com.google.android.maps.MyLocationOverlay;
import com.google.android.maps.Overlay;

public class CameraMapActivity extends MapActivity {
    private MyLocationOverlay mMyLocationOverlay;
    private CameraMapOverlay mItemizedoverlay;
    private MapView mMapView;

    private static final int MYLOCATION_MENU_ID = MainActivity.LAAT_USED_MENUID;    
    
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.map_layout);

        MapView mapView = (MapView) findViewById(R.id.mapview);
        mMapView = mapView;
        mapView.setBuiltInZoomControls(true);
        
        List<Overlay> mapOverlays = mapView.getOverlays();
        Drawable drawable = this.getResources().getDrawable(R.drawable.icon);
        drawable = this.getResources().getDrawable(android.R.drawable.ic_menu_mylocation);
        mItemizedoverlay = new CameraMapOverlay(drawable);
        
        addCameraMarkers();

        mMyLocationOverlay = new MyLocationOverlay(getApplicationContext(), mapView);
        
        mapOverlays.add(mItemizedoverlay);
        mapOverlays.add(mMyLocationOverlay);
        
        mItemizedoverlay.setOnTapListener(new OnTapListener() {
            
            @Override
            public void onTap(CameraOverlayItem item) {
                long id = item.getId();
                Context context = CameraMapActivity.this;
                Intent launchIntent = new Intent(context,
                        CameraPictureActivity.class);
                launchIntent.putExtra(CameraPictureActivity.EXTRA_ID, id);
                context.startActivity(launchIntent);
            }
        });
        
    }
    
    @Override
    protected void onResume(){
        super.onResume();
        
        mMyLocationOverlay.enableCompass();
        mMyLocationOverlay.enableMyLocation();
    }
    
    @Override
    protected void onPause(){
        super.onPause();

        mMyLocationOverlay.disableCompass();
        mMyLocationOverlay.disableMyLocation();
    }
    
	private void addCameraMarkers() {
        CameraMapOverlay itemizedoverlay = mItemizedoverlay;

        itemizedoverlay.clearOverlayItems();
        
        Cursor cursor = getCursor();
        Resources resources = getResources();
        int longCol = cursor.getColumnIndexOrThrow(CameraDefs.LONGITUDE); 
        int latCol = cursor.getColumnIndexOrThrow(CameraDefs.LATITUDE);
        int idCol = cursor.getColumnIndexOrThrow(CameraDefs.ID);
        while (cursor.moveToNext()){
            Double longitude = cursor.getDouble(longCol);
            Double latitude  = cursor.getDouble(latCol);
            long id           = cursor.getLong(idCol);
            
            addCamera(itemizedoverlay, resources, longitude, latitude, id);
        }
        
        itemizedoverlay.doPopulate();
        cursor.close();
	}

	private Cursor getCursor() {
		String[] projection = new String[] { 
				CameraDefs.LONGITUDE,
				CameraDefs.LATITUDE,
				CameraDefs.ID };
		
		String filter = CameraDefs.LATITUDE + " IS NOT NULL AND " + CameraDefs.LONGITUDE + " IS NOT NULL";
		
        return managedQuery(
        		CameraContentProvider.CONTENT_URI, 
        		projection, 
        		filter, 
        		null, 
        		null);
	}

	private void addCamera(CameraMapOverlay itemizedoverlay,
			Resources resources, Double longitude, Double latitude, long id) {
        //Drawable icon = resources.getDrawable(R.drawable.icon);
        CameraOverlayItem overlayitem = new CameraOverlayItem(id, longitude, latitude, null);
        
        itemizedoverlay.addOverlayItem(overlayitem);
		
	}

	@Override
	protected boolean isRouteDisplayed() {
		return false;
	}
	
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        //Log.d(LOGTAG, "In onCreateOptionsMenu");
        
        boolean result = super.onCreateOptionsMenu(menu);

        menu.add(0, MYLOCATION_MENU_ID, 0, R.string.MyPosition)
            .setIcon(android.R.drawable.ic_menu_mylocation);
        
        return result;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        //Log.d(LOGTAG, "In onOptionsItemSelected");
        
        switch (item.getItemId()) {
        case MYLOCATION_MENU_ID:
            panToCurrentLocation();
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
    private void panToCurrentLocation(){
        GeoPoint point = mMyLocationOverlay.getMyLocation();
        if (point != null)
            setCenter(point);
        else
        	Toast.makeText(this, R.string.NoPositionFound, Toast.LENGTH_SHORT).show();
    }

    protected void setCenter(GeoPoint point) {
        mMapView.getController().setCenter(point);
    }
}
